//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.indexes.couponlist;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.TableShell;
import com.nebutown.commonunits.dataface.definitions.ButtonField;
import com.nebutown.commonunits.dataface.definitions.FieldValue;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.dataface.definitions.PageInfo;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.coupon.support.CouponData;
import com.restaurant.application.indexes.couponlist.mysql.CouponListBean;
import com.restaurant.application.indexes.couponlist.mysql.CouponListCriteria;
import com.restaurant.application.indexes.couponlist.mysql.CouponListImpl;
import com.restaurant.application.indexes.couponlist.support.AbstractAssistCouponList;
import com.restaurant.application.indexes.couponlist.support.SearchData;
import com.restaurant.application.minifacade.data.CardBO;
import com.restaurant.application.minifacade.data.CouponListPadBO;
import com.restaurant.application.minifacade.data.CouponPadBO;
import com.restaurant.application.minifacade.data.IdBO;
import com.restaurant.application.util.CommonUtils;
import java.util.List;
import java.util.Objects;

public class AssistCouponListImpl extends AbstractAssistCouponList {
    public AssistCouponListImpl() {
    }

    public View getFullView() {
        return this.newCommonTable(new SearchData());
    }

    public View getViewLead(ViewSpec viewSpec) {
        return this.newCommonTable(new SearchData());
    }

    public ViewUpdate search(SearchData search, int[] seq) {
        return this.returnView(new View[]{this.newCommonTable(search)});
    }

    private String getWhere(SearchData search) {
        StringBuilder builder = new StringBuilder();
        builder.append(" true");
        if (!Strings.isNullOrEmpty(search.getModality())) {
            builder.append(" and MODALITY like '%").append(search.getModality()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getNumber())) {
            builder.append(" and NUMBER like '%").append(search.getNumber()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getCouponState())) {
            builder.append(" and COUPONSTATE = '").append(search.getCouponState()).append("'");
        }

        if (!Strings.isNullOrEmpty(search.getState())) {
            builder.append(" and STATE = '").append(search.getState()).append("'");
        }

        if (Objects.nonNull(search.getCreateTime())) {
            if (Objects.nonNull(search.getCreateTime().getFrom()) && search.getCreateTime().getFrom() > 0L) {
                builder.append(" and CREATETIME >= ").append(search.getCreateTime().getFrom());
            }

            if (Objects.nonNull(search.getCreateTime().getTo()) && search.getCreateTime().getTo() > 0L) {
                builder.append(" and CREATETIME < ").append(search.getCreateTime().getTo());
            }
        }

        if (Objects.nonNull(search.getUsedTime())) {
            if (Objects.nonNull(search.getUsedTime().getFrom()) && search.getUsedTime().getFrom() > 0L) {
                builder.append(" and USEDTIME >= ").append(search.getUsedTime().getFrom());
            }

            if (Objects.nonNull(search.getUsedTime().getTo()) && search.getUsedTime().getTo() > 0L) {
                builder.append(" and USEDTIME < ").append(search.getUsedTime().getTo());
            }
        }

        if (!Strings.isNullOrEmpty(search.getCustomerId())) {
            builder.append(" and CUSTOMERID like '%").append(search.getCustomerId()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getPhone())) {
            builder.append(" and PHONE like '%").append(search.getPhone()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getUsedCustomerId())) {
            builder.append(" and USEDCUSTOMERID like '%").append(search.getUsedCustomerId()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getCustomerUid())) {
            builder.append(" and CURRENTCUSTOMERUID = '").append(search.getCustomerUid()).append("'");
        }

        if (!Strings.isNullOrEmpty(search.getUsedPhone())) {
            builder.append(" and USEDPHONE like '%").append(search.getUsedPhone()).append("%'");
        }

        builder.append(" ORDER BY CREATETIME DESC, RECORD_ID ASC");
        return builder.toString();
    }

    public List<CardBO> getMyCardBeanList(UID customerUid, String status, String usedStatus, IdBO bo) {
        List<CardBO> cardBeans = Lists.newLinkedList();
        if (Objects.nonNull(customerUid) && Objects.nonNull(bo) && bo.getPageSize() > 0 && bo.getPage() > 0) {
            String where = this.getWhere(customerUid, status, usedStatus);
            List<CouponListBean> beans = ((CouponListImpl)this.getIndex(CouponListImpl.class)).search(where, (bo.getPage() - 1) * bo.getPageSize(), bo.getPage() * bo.getPageSize());
            if (beans.size() > 0) {
                beans.forEach((e) -> {
                    cardBeans.add(this.getCardBean(e));
                });
            }
        }

        return cardBeans;
    }

    private String getWhere(UID customerUid, String status, String usedStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append(" true");
        if (!Strings.isNullOrEmpty(status) && !Strings.isNullOrEmpty(usedStatus)) {
            if ("有效".equals(status) && "未使用".equals(usedStatus)) {
                builder.append(" and STATE = '").append(status).append("'").append(" and COUPONSTATE = '").append(usedStatus).append("'");
            } else {
                builder.append(" and (STATE = '").append(status).append("'").append(" or COUPONSTATE = '").append(usedStatus).append("')");
            }
        }

        if (Objects.nonNull(customerUid)) {
            builder.append(" and CURRENTCUSTOMERUID = '").append(customerUid).append("'");
        }

        builder.append(" ORDER BY ENDTIME ASC, RECORD_ID ASC");
        return builder.toString();
    }

    public UID getMyCardUid(String number) {
        List<CouponListBean> beans = ((CouponListImpl)this.getIndex(CouponListImpl.class)).search((new CouponListCriteria()).withNumberEquals(number), (SortSpec)null, 1);
        return beans.size() > 0 ? ((CouponListBean)beans.get(0)).getCouponUid() : null;
    }

    public CouponListPadBO getCouponListPadBO(SearchData search, Integer pageSize) {
        CouponListPadBO bo = new CouponListPadBO();
        if (Objects.nonNull(search) && Objects.nonNull(search.getPage()) && search.getPage() > 0 && Objects.nonNull(pageSize) && pageSize > 0) {
            int page = search.getPage();
            String where = this.getWhere(search);
            List<CouponListBean> beans = ((CouponListImpl)this.getIndex(CouponListImpl.class)).search(where, (page - 1) * pageSize, page * pageSize);
            List<CouponPadBO> list = Lists.newArrayList();
            if (beans.size() > 0) {
                beans.forEach((e) -> {
                    list.add(this.getCouponPadBO(e));
                });
            }

            long totalRecord = (long)((CouponListImpl)this.getIndex(CouponListImpl.class)).count(where);
            bo.withLists(list).withTotalRecord(totalRecord);
        }

        return bo;
    }

    private CouponPadBO getCouponPadBO(CouponListBean bean) {
        return (new CouponPadBO()).withName(bean.getModality()).withUsedTime(Objects.nonNull(bean.getUsedTime()) && bean.getUsedTime() > 0L ? DateTimeUtils.toTime(bean.getUsedTime()) : "").withUsedPhone(bean.getUsedPhone()).withNumber(bean.getNumber());
    }

    private CardBO getCardBean(CouponListBean bean) {
        return (new CardBO()).withCardId(bean.getNumber()).withContent(bean.getModality()).withEndTime(DateTimeUtils.toTime(bean.getEndTime())).withErCode(bean.getQrCodeUrl()).withRemark(bean.getRemarks()).withFullMoney(bean.getFullMoney()).withMinusMoney(bean.getMinusMoney()).withCardType(bean.getType()).withState(bean.getState()).withStatus(bean.getCouponState());
    }

    private View newCommonTable(SearchData search) {
        String where = this.getWhere(search);
        int page = 1;
        if (Objects.nonNull(search.getPage()) && search.getPage() > 1) {
            page = search.getPage();
        }

        List<CouponListBean> beans = ((CouponListImpl)this.getIndex(CouponListImpl.class)).search(where, (page - 1) * 10, page * 10);
        int totalRecord = ((CouponListImpl)this.getIndex(CouponListImpl.class)).count(where);
        PageInfo info = CommonUtils.getPageInfo(page, 10, totalRecord, this, CMD_SEARCH, "page");
        PageForm pageForm = this.newFormShell(search, beans, info).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm);
    }

    private FormShell newFormShell(SearchData search, List<CouponListBean> beans, PageInfo info) {
        FormShell shell = new FormShell("优惠券/使用记录", 3, 5);
        shell.source(search, DataModes.EDIT);
        shell.at(0, 0, "券名").large(1, 1).stringField("modality", search.getModality()).placeHolder("券名").setEditable(true);
        shell.at(0, 1, "券号").large(1, 1).stringField("number", search.getNumber()).placeHolder("券号").setEditable(true);
        shell.at(0, 2, "优惠券状态").large(1, 1).selectEntry("state", search.getState(), "", new String[]{"已过期", "有效"}).placeHolder("优惠券状态").setEditable(true);
        shell.at(1, 0, "使用状态").large(1, 1).selectEntry("couponState", search.getCouponState(), "", new String[]{"已使用", "未使用"}).placeHolder("使用状态").setEditable(true);
        shell.at(1, 1, "领用时间").intervalField("createTime", search.getCreateTime(), DateTypes.DATETIME).notNull().setEditable(true);
        shell.at(1, 2, "使用时间").intervalField("usedTime", search.getUsedTime(), DateTypes.DATETIME).notNull().setEditable(true);
        shell.at(2, 0, "领用人编号").large(1, 1).stringField("customerId", search.getCustomerId()).placeHolder("领用人编号").setEditable(true);
        shell.at(2, 1, "领用人手机").large(1, 1).stringField("phone", search.getPhone()).placeHolder("领用人手机").setEditable(true);
        shell.at(2, 2, "使用人编号").large(1, 1).stringField("usedCustomerId", search.getUsedCustomerId()).placeHolder("使用人编号").setEditable(true);
        shell.at(3, 0, "使用人手机").large(1, 1).stringField("usedPhone", search.getUsedPhone()).placeHolder("使用人手机").setEditable(true);
        List<ButtonField> buttons = Lists.newArrayList();
        buttons.add(new ButtonField(this.getPath(CMD_SEARCH, new int[0]), "查询", true, "", false));
        shell.at(4, 0, (String)null).buttonBoxField((ButtonField[])buttons.toArray(new ButtonField[0]));
        TableShell<CouponListBean> tableShell = new TableShell(CouponListBean.class, beans);
        tableShell.createColumn("createTime").withName("领取时间").withWidth(120).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(DateTimeUtils.toTime(bean.getCreateTime()));
        });
        tableShell.createColumn("customerId").withName("领用人编号").withWidth(110).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getCustomerId());
        });
        tableShell.createColumn("phone").withName("手机").withWidth(120).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getPhone());
        });
        tableShell.createColumn("type").withName("类型").withWidth(75).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getType());
        });
        tableShell.createColumn("modality").withName("券名").withWidth(128).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getModality());
        });
        tableShell.createColumn("id").withName("券号").withWidth(115).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getNumber());
        });
        tableShell.createColumn("state").withName("优惠券状态").withWidth(100).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getState());
        });
        tableShell.createColumn("couponState").withName("使用状态").withWidth(90).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getCouponState());
        });
        tableShell.createColumn("usedTime").withName("使用日期").withWidth(120).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getUsedTime() > 0L ? DateTimeUtils.toTime(bean.getUsedTime()) : "");
        });
        tableShell.createColumn("usedCustomerId").withName("使用人编号").withWidth(110).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getUsedCustomerId());
        });
        tableShell.createColumn("usedPhone").withName("使用人手机").withWidth(120).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getUsedPhone());
        });
        tableShell.createColumn("userId").withName("核销人").withWidth(120).cellCalculator((e) -> {
            CouponListBean bean = (CouponListBean)e;
            return (new FieldValue()).withValue(bean.getUserId());
        });
        tableShell.withPageInfo(info);
        shell.at(5, 0, "优惠券/使用记录").large(3, 1).tableField("couponList", tableShell);
        return shell;
    }

    public CouponListBean createBean(UID uid, CouponData data) {
        if (!Objects.isNull(uid) && !Objects.isNull(data)) {
            String shopNameList = null;
            if (data.getShopNameList().size() > 0) {
                shopNameList = String.join(",", data.getShopNameList());
            }

            return (new CouponListBean()).withCouponUid(uid).withCouponManageUid(data.getCouponManageUid().value()).withNumber(data.getNumber()).withEndTime(data.getEndTime()).withType(data.getType()).withModality(data.getModality()).withFullMoney(data.getFullMoney()).withMinusMoney(data.getMinusMoney()).withShopNameList(shopNameList).withRemarks(data.getRemarks()).withState(data.getState()).withId(data.getId()).withCreateTime(data.getCreateTime()).withModifyTime(data.getModifyTime()).withCustomerId(data.getCustomerId()).withCustomerName(data.getCustomerName()).withCustomerUid(Objects.isNull(data.getCustomerUid()) ? null : data.getCustomerUid().value()).withPhone(data.getPhone()).withQrCodeUrl(data.getQrCodeUrl()).withQrCodeUid(data.getQrCodeUid().value()).withCouponState(data.getCouponState()).withCurrentCustomerId(data.getCurrentCustomerId()).withCurrentCustomerName(data.getCurrentCustomerName()).withCurrentCustomerUid(Objects.isNull(data.getCurrentCustomerUid()) ? null : data.getCurrentCustomerUid().value()).withCurrentPhone(data.getCurrentPhone()).withUserId(data.getUserId()).withUserName(data.getUserName()).withUserUid(Objects.isNull(data.getUserUid()) ? null : data.getUserUid().value()).withUsedTime(data.getUsedTime());
        } else {
            return null;
        }
    }

    public CouponListBean updateBean(UID uid, CouponData data, CouponListBean bean) {
        if (!Objects.isNull(uid) && !Objects.isNull(data) && !Objects.isNull(bean)) {
            bean.withCouponUid(uid);
            if (Objects.nonNull(data.getEndTime())) {
                bean.withEndTime(data.getEndTime());
            }

            if (!Strings.isNullOrEmpty(data.getRemarks())) {
                bean.withRemarks(data.getRemarks());
            }

            if (Objects.nonNull(data.getState())) {
                bean.withState(data.getState());
            }

            if (Objects.nonNull(data.getModifyTime())) {
                bean.withModifyTime(data.getModifyTime());
            }

            if (!Strings.isNullOrEmpty(data.getCustomerId())) {
                bean.withCustomerId(data.getCustomerId());
            }

            if (!Strings.isNullOrEmpty(data.getCustomerName())) {
                bean.withCustomerName(data.getCustomerName());
            }

            if (Objects.nonNull(data.getCustomerUid())) {
                bean.withCustomerUid(data.getCustomerUid().value());
            }

            if (!Strings.isNullOrEmpty(data.getPhone())) {
                bean.withPhone(data.getPhone());
            }

            if (!Strings.isNullOrEmpty(data.getCouponState())) {
                bean.withCouponState(data.getCouponState());
            }

            if (!Strings.isNullOrEmpty(data.getCurrentCustomerId())) {
                bean.withCurrentCustomerId(data.getCurrentCustomerId());
            }

            if (Objects.nonNull(data.getCurrentCustomerUid())) {
                bean.withCurrentCustomerUid(data.getCurrentCustomerUid().value());
            }

            if (!Strings.isNullOrEmpty(data.getCurrentCustomerName())) {
                bean.withCurrentCustomerName(data.getCurrentCustomerName());
            }

            if (!Strings.isNullOrEmpty(data.getCurrentPhone())) {
                bean.withCurrentPhone(data.getCurrentPhone());
            }

            if (Objects.nonNull(data.getUsedTime())) {
                bean.withUsedTime(data.getUsedTime());
            }

            if (!Strings.isNullOrEmpty(data.getUserId())) {
                bean.withUserId(data.getUserId());
            }

            if (!Strings.isNullOrEmpty(data.getUserName())) {
                bean.withUserName(data.getUserName());
            }

            if (Objects.nonNull(data.getUserUid())) {
                bean.withUserUid(data.getUserUid().value());
            }

            if (Objects.nonNull(data.getUsedCustomerUid())) {
                bean.withUsedCustomerUid(data.getUsedCustomerUid().value());
            }

            if (!Strings.isNullOrEmpty(data.getUsedCustomerId())) {
                bean.withUsedCustomerId(data.getUsedCustomerId());
            }

            if (!Strings.isNullOrEmpty(data.getUsedCustomerName())) {
                bean.withUsedCustomerName(data.getUsedCustomerName());
            }

            if (!Strings.isNullOrEmpty(data.getUsedPhone())) {
                bean.withUsedPhone(data.getUsedPhone());
            }

            return bean;
        } else {
            return null;
        }
    }

    @Override
    public List<CardBO> getMyCardBeanList(UID customerUid, String status, String useStatus) {
        return null;
    }
}
